<template>
	<view class="user-dynamic-info">
		<view class="user-dynamic-info-1">
			<image :src="userInfo.heard_img"></image>
			<view class="user-dynamic-info-1-box">
				<view class="user-dynamic-info-1-name">用户名：{{userInfo.nickname}}</view>
				<view class="user-dynamic-info-1-id">ID:{{user_id}}</view>
				<view class="user-dynamic-info-1-phone">联系方式：{{userInfo.phone}}</view>
			</view>
			<view>
				<text class="phoneCall" @click="phoneCall(userInfo.phone)">呼叫</text>
				<text class="phoneCall tag" @click="setTag" v-if="currentUserInfo.role_type==1">{{tagsName}}</text>
			</view>
		</view>
		<view class="user-dynamic-info-remark" v-if="currentUserInfo.role_type==2 || currentUserInfo.role_type==3 || currentUserInfo.role_type==4 || currentUserInfo.role_type==11">
			<view class="label">跟进备注</view>
			<view class="btn" @click="addRemark">+新增</view>
		</view>
		<view class="remark-list" v-if="userInfo.remark.length">
			<view class="input-list" v-for="(data,index) in userInfo.remark" :key='index'>
				<view class="time-name">
					<view class="time">{{data.add_time}}</view>
					<view class="name">{{data.nickname}}</view>
				</view>
				<input placeholder="请输入备注" v-model="data.remark" :disabled="data.remark?true:false" confirm-type="done"/>
			</view>
		</view>
		<view class="user-dynamic-datetype">
			<view v-for="(data,index) in dateType" :class="{active: dateIndex===index}" @click="selectDatetype(index)" :key="index">{{data.label}}</view>
		</view>
		<view class="user-dynamic-info-2">
			<view class="user-dynamic-info-2-tit">用户使用情况（次数）</view>
			<view class="user-dynamic-info-2-list">
				<view class="user-dynamic-info-2-item">
					<view>小程序</view>
					<text>{{all_browse[0]}}</text>
				</view>
				<view class="user-dynamic-info-2-item">
					<view>APP</view>
					<text>{{all_browse[1]}}</text>
				</view>
				<view class="user-dynamic-info-2-item">
					<view>WEB</view>
					<text>{{all_browse[2]}}</text>
				</view>
				<view class="user-dynamic-info-2-item">
					<view>电脑端</view>
					<text>{{all_browse[3]}}</text>
				</view>
				<view class="user-dynamic-info-2-item">
					<view>Ipad</view>
					<text>{{all_browse[4]}}</text>
				</view>
			</view>
		</view>
		<u-modal v-model="showRemarkPop" title="跟进备注" :title-style="{paddingTop: '30rpx'}" :show-cancel-button="true" @confirm="setRemark">
			<view class="remark-pop">
				<input placeholder="请输入跟进备注" v-model="remark" />
			</view>
		</u-modal>
		<view>
			<view :class="['user-dynamic-info-3',{'fixed': fixed}]" ref="tabs">
				<tabs :list="tabs" v-model="active" itemColor="#333" lineColor="#2073F5" @input="select" :flex="true"></tabs>
			</view>
			<view class="user-dynamic-info-list" v-if="active==0">
				<block v-if="dynamicList.length">
					<view class="user-dynamic-item" v-for="(data,index) in dynamicList" :key="index">
						<view class="user-dynamic-item-tims"><text class="blue">{{data.foot_where}}</text>浏览次数：{{data.num}}次</view>
						<view class="user-dynamic-item-tit">
							<text v-if="data.type==1">小程序</text>
							<text v-if="data.type==2">APP</text>
							<text v-if="data.type==3">H5</text>
							<text v-if="data.type==4">PC</text>
							<text v-if="data.type==5">Ipad</text>
							<text v-if="data.type==6">后台系统</text>
							<text>{{data.add_time}}</text>
						</view>
					</view>
				</block>
				<block v-else><no-more :text="'暂无数据'"></no-more></block>
			</view>
			<view class="user-foot-info-list" v-else>
				<block v-if="footList.length">
					<view class="user-dynamic-item" v-for="(data,index) in footList" :key="index">
						<image :src="data.image_middle"></image>
						<view class="user-dynamic-item-info">
							<view class="title">{{data.goods_name}}</view>
							<view class="price">￥<text>{{data.retail_price}}</text></view>
							<view class="times">浏览次数:{{data.num}}次 <text>{{data.add_time}}</text></view>
						</view>
					</view>
				</block>
				<block v-else><no-more :text="'暂无数据'"></no-more></block>
			</view>
			<view v-if="(dynamicList.length || footList.length) && noMore">
				<no-more :text="'数据加载完毕'"></no-more>
			</view>
			<u-select v-model="showTags" :list="tagList" label-name="group_name" value-name="group_id" @confirm="confirmSelectTag"></u-select>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dateIndex: 0,
				dateType: [{
					label: '全部',
					value: ''
				},{
					label: '今天',
					value: 'today'
				},{
					label: '昨天',
					value: 'yestoday'
				},{
					label: '本周',
					value: 'week'
				},{
					label: '上周',
					value: 'lastweek'
				},{
					label: '本月',
					value: 'month'
				},{
					label: '上月',
					value: 'lastmonth'
				},{
					label: '今年',
					value: 'year'
				},{
					label: '去年',
					value: 'lastyear'
				}],
				all_browse: [0,0,0,0,0],
				userInfo: {
					remark: [],
					nickname: '',
					heard_img: '',
					phone: ''
				},
				noMore: false,
				user_id: '',
				page: 0,
				active: 0,
				tabs: [{title: '用户动态'},{title: '用户足迹'}],
				fixed: false,
				offsetTop: 0,
				dynamicList: [],
				footList: [],
				currentUserInfo: JSON.parse(uni.getStorageSync('userInfo')),
				remark: '',
				showRemarkPop: false,
				showTags: false,
				tagList: [],
				tagsName: '选择分组',
				isSubmit: false
			};
		},
		onLoad(options) {
			this.user_id = options.user_id
			this.active = options.type
		},
		onShow() {
			this.init(this.page,this.user_id)
			this.getUserInfo()
		},
		onPageScroll(e){
			if(e.scrollTop >= this.offsetTop){
				this.fixed = true
			}else{
				this.fixed = false
			}
		},
		mounted(){
			let _this = this
			const query = uni.createSelectorQuery().in(this)
			query.select('.user-dynamic-info-3').boundingClientRect(res => {
				_this.offsetTop = res.top
			}).exec();
		},
		onReachBottom() {
			if(!this.noMore){
				this.page++
				this.init(this.page,this.user_id)
			}
		},
		methods: {
			async setTag(){
				const { datas } = await this.$api.SelectStoreGroup()
				this.tagList = datas
				this.showTags = true
			},
			async confirmSelectTag(e){
				if(e[0].value){
					const { datas } = await this.$api.UserStoreGroup({
						user_id: this.user_id,
						group_id: e[0].value
					})
					uni.showToast({
						icon: 'none',
						title: '设置成功'
					})
					this.getUserInfo()
				}
			},
			getUserInfo(){
				this.$api.UserList({user_id: this.user_id}).then(res => {
					if(res.datas[0]){
						this.userInfo = {
							remark: res.datas[0].remark,
							nickname: res.datas[0].nickname,
							heard_img: res.datas[0].heard_img,
							phone: res.datas[0].phone
						}
						if(res.datas[0].store_group_name){
							this.tagsName = res.datas[0].store_group_name
						}
					}
				})
			},
			init(page,user_id){
				let data = {
					asc: 2,
					order_by: 'add_time',
					page: page?page:0,
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
					user_id: user_id,
					is_all: 1,
					time: this.dateIndex?this.dateType[this.dateIndex].value:''
				}
				this.$api.BrowseUserList(data).then(res => {
					if(res.datas.all_browse.length){
						res.datas.all_browse.forEach(item => {
							switch (Number(item.type)){
								case 1:
									this.$set(this.all_browse,0,item.num)
									break;
								case 2:
									this.$set(this.all_browse,1,item.num)
									break;
								case 3:
									this.$set(this.all_browse,2,item.num)
									break;
								case 4:
									this.$set(this.all_browse,3,item.num)
									break;
								case 5:
									this.$set(this.all_browse,4,item.num)
									break;
							}
						})
					}
				})
				if(this.active==0){
					this.$api.BrowseUserList(data).then(res => {
						if(res.datas.list.length){
							res.datas.list.forEach(item => {
								this.dynamicList.push(item)
							})
						}else{
							this.noMore = true
						}
					})
				}else{
					let BrowseListData = {
						asc: 2,
						order_by: 'add_time',
						page: page?page:0,
						store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
						user_id: user_id,
						paramter: 1,
						time: this.dateIndex?this.dateType[this.dateIndex].value:''
					}
					this.$api.BrowseList(BrowseListData).then(res => {
						if(res.datas.list.length){
							res.datas.list.forEach(item => {
								this.footList.push(item)
							})
						}else{
							this.noMore = true
						}
					})
				}
			},
			selectDatetype(index){
				this.dateIndex = index
				this.noMore = false
				this.dynamicList = []
				this.footList = []
				this.page = 0
				this.init(this.page,this.user_id)
			},
			select(e){
				this.noMore = false
				this.dynamicList = []
				this.footList = []
				this.page = 0
				this.init(this.page,this.user_id)
			},
			phoneCall(phone){
				if(phone.length!=11){
					uni.showToast({
						icon: 'none',
						title: '电话号码不存在',
						duration: 1500
					})
					return false
				}
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			addRemark(){
				this.showRemarkPop = true
			},
			// 设置备注
			async setRemark(){
				if(this.isSubmit) return
				this.isSubmit = true
				const { datas } = await this.$api.UserRemark({
					user_id: this.user_id,
					remark: this.remark
				}).catch(() => {
					setTimeout(() => {
						this.isSubmit = false
					},2000)
				})
				this.getUserInfo()
				setTimeout(() => {
					this.isSubmit = false
				},2000)
			},
		}
	}
</script>

<style lang="scss">
	.user-dynamic-info{
		padding-top: 20rpx;
		.user-dynamic-info-remark{
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx 30rpx;
			background-color: #FFF;
			.label{
				font-size: 24rpx;
			}
			.btn{
				border-radius: 8rpx;
				padding: 10rpx 20rpx;
				background-color: #2073F5;
				color: #FFF;
				font-size: 24rpx;
			}
		}
		.user-dynamic-info-1{
			background-color: #FFF;
			display: flex;
			align-items: center;
			padding: 30rpx 20rpx;
			image{
				width: 130rpx;
				height: 130rpx;
				border-radius: 100%;
				background-color: #EEE;
			}
			.phoneCall{
				text-align: center;
				display: block;
				color: #2073F5;
				border: solid 1px #2073F5;
				font-size: 28rpx;
				padding: 5rpx 40rpx;
				border-radius: 40rpx;
				&.tag{
					background-color: #2073F5;
					color: #FFF;
					margin-top: 20rpx;
				}
			}
			.user-dynamic-info-1-box{
				flex: 1;
				min-width: 0;
				margin-left: 20rpx;
				.user-dynamic-info-1-name{
					font-size: 30rpx;
					color: #333;
				}
				.user-dynamic-info-1-id{
					font-size: 26rpx;
					color: #999;
					margin: 10rpx 0;
				}
				.user-dynamic-info-1-phone{
					font-size: 28rpx;
				}
			}
		}
		.user-dynamic-datetype{
			display: flex;
			align-items: center;
			padding: 30rpx 30rpx 0;
			view{
				flex: 1;
				font-size: 24rpx;
				text-align: center;
				height: 54rpx;
				line-height: 54rpx;
				background-color: #FFF;
				&:first-child{
					border-radius: 8rpx 0 0 8rpx;
				}
				&:last-child{
					border-radius: 0 8rpx 8rpx 0;
				}
				&.active{
					background-color: #2073F5;
					color: #FFF;
				}
			}
		}
		.user-dynamic-info-2{
			background-color: #FFF;
			margin-top: 20rpx;
			padding: 30rpx 20rpx;
			border-bottom: solid 1px #EEE;
			.user-dynamic-info-2-tit{
				font-size: 30rpx;
			}
			.user-dynamic-info-2-list{
				display: flex;
				margin-top: 30rpx;
				.user-dynamic-info-2-item{
					flex: 1;
					text-align: center;
					view{
						font-size: 24rpx;
						color: #999;
						text-transform: uppercase;
						margin-bottom: 20rpx;
					}
					text{
						font-size: 28rpx;
						font-weight: bold;
					}
				}
			}
		}
		.user-dynamic-info-list,.user-foot-info-list{
			margin-top: 20rpx;
		}
		.user-dynamic-info-list .user-dynamic-item{
			position: relative;
			padding: 40rpx 20rpx;
			background-color: #FFF;
			&::after{
				position: absolute;
				bottom: 0;
				left: 20rpx;
				right: 20rpx;
				height: 1px;
				content: '';
				background-color: #EEE;
			}
			.user-dynamic-item-tit{
				display: flex;
				justify-content: space-between;
				margin-bottom: 10rpx;
				font-size: 30rpx;
				align-items: center;
				text:last-child{
					color: #141414;
					font-weight: bold;
					font-size: 22rpx;
				}
			}
			.user-dynamic-item-tims{
				margin-bottom: 10rpx;
				font-size: 26rpx;
				color: #666;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.blue{
					color: #2073F5;
				}
			}
		}
		.user-foot-info-list .user-dynamic-item{
			padding: 20rpx;
			background-color: #FFF;
			display: flex;
			display: flex;
			image{
				width: 250rpx;
				height: 250rpx;
				border: solid 1px #EEE;
			}
			.user-dynamic-item-info{
				flex: 1;
				min-width: 0;
				position: relative;
				height: 250rpx;
				margin-left: 20rpx;
				position: relative;
				view{
					display: block;
					&.title{
						height: 120rpx;
						overflow : hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						font-size: 28rpx;
					}
					&.price{
						font-size: 20rpx;
						text{
							font-size: 28rpx;
							color: #333;
						}
					}
					&.times{
						position: absolute;
						bottom: 10rpx;
						left: 0;
						right: 0;
						font-size: 22rpx;
						color: #666;
						margin-top: 10rpx;
						display: flex;
						justify-content: space-between;
					}
				}
			}
		}
		.remark-list{
			background-color: #FFF;
			.input-list{
				padding: 0 0 30rpx;
				.time-name{
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 30rpx 10rpx;
					font-size: 24rpx;
					.time{
						color: red;
						font-size: 24rpx;
					}
					.name{
						font-size: 24rpx;
						font-weight: bold;
					}
				}
				input{
					font-size: 24rpx;
					padding: 10rpx 30rpx;
				}
			}
		}
	}
	.remark-pop{
		padding: 30rpx;
		input{
			
		}
	}
</style>
